package com.hnkywl.sts.dao.sfgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.sfgl.Sfk;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sfgl
 * 类描述:    付款审核dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class FkshDao extends SimpleHibernateTemplate<Sfk, Long> {
    /**
     * 查询付款审核
     *
     * @param bm
     * @param sbgsMc
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Sfk> findFklxList(Date fkshStart, Date fkshEnd, String bm, String sbgsMc, Long nbgsId, String gldwMc, String zt, String fklx, String zdr,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Sfk> page = new Page<Sfk>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (bm != null && !bm.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" bm like ? ");
            param.add("%" + bm.trim() + "%");
        }
        if (zdr != null && !zdr.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" createUser.xm like ? ");
            param.add("%" + zdr.trim() + "%");
        }
        if (gldwMc != null && !gldwMc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gldw.qc like ? ");
            param.add("%" + gldwMc.trim() + "%");
        }
        if (nbgsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sbgs.id = ? ");
            param.add(nbgsId);
        }
        if (nbgsId == null && sbgsMc != null && !sbgsMc.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sbgs.jc like ? ");
            param.add("%" + sbgsMc.trim() + "%");
        }

        if (zt != null && !zt.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" zt = ? ");
            param.add(zt);
        }
        if (fklx != null && !fklx.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sfwsyfk = ? ");
            param.add(Integer.parseInt(fklx));
        }
        if (fkshStart != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated >= ? ");
            param.add(sDaysAfterNowDate(fkshStart));
        }
        if (fkshEnd != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated < ? ");
            param.add(jDaysAfterNowDate(fkshEnd));
        }
        //==============记录数量===============
        hql = "select count(*) from Sfk where sfklx in (1,2) and  sftpfy = 'ftpfk' and sfBzj != 1  " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Sfk where sfklx in (1,2) and  sftpfy = 'ftpfk' and sfBzj != 1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by dateCreated desc ";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public String findAllfksh() {
        String hql = "select count(*) from Sfk where sfklx in (1,2) and  sftpfy = 'ftpfk' and zt='ybc' " + RegionDao.createDataRegion("and");
        Long c = (Long) findUnique(hql);
        return c.toString();
    }

    public Date jDaysAfterNowDate(Date date) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.set(java.util.Calendar.HOUR_OF_DAY, 23);
        rightNow.set(java.util.Calendar.MINUTE, 59);
        rightNow.set(java.util.Calendar.SECOND, 59);
        return rightNow.getTime();
    }

    //   当前日期加减n天后的日期，返回String   (yyyy-mm-dd)
    public Date sDaysAfterNowDate(Date date) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.set(java.util.Calendar.HOUR_OF_DAY, 0);
        rightNow.set(java.util.Calendar.MINUTE, 0);
        rightNow.set(java.util.Calendar.SECOND, 0);
        return rightNow.getTime();
    }


    /**
     * 按照采购合同查询付款审核
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Sfk> findfkshBySfkList(Long sfkId, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Sfk> page = new Page<Sfk>(pageNo, pageSize, 0);

        if (sfkId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" id = ? ");
            param.add(sfkId);
        }
        //==============记录数量===============
        hql = "select count(*) from Sfk where sfklx in (1,2)" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Sfk where sfklx in (1,2)" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by sfkrq desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }


    public Sfk getFksh(Long sfkId) {
        String hql = "from Sfk where sfklx in (1,2) and id = ?";
        return (Sfk) find(hql, sfkId).get(0);
    }

    /**
     * 判断属性是否重复
     *
     * @param id            付款审核id
     * @param propertyName  付款审核属性名
     * @param propertyValue 付款审核属性值
     * @return
     */
    public Boolean checkUnique(Long id, String propertyName, Object propertyValue) {
        String hql;
        if (propertyValue == null)
            return false;
        if (id == null) {
            //修改检验
            hql = "from Sfk where sfklx in (1,2) and " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        } else {
            //新增检验
            hql = "from Sfk where sfklx in (1,2) and " + propertyName + " = ? and id <>?";
            return this.existsQuery(hql, propertyValue, id);
        }
    }

    public List<Map> findAllDbsfrw(Long gsid){
        String sql = " select \'内部划转审核流程\' as mc,fk.bm as fkbm,sk.bm as skbm,sk.date_created as cjrq,yg.dlm as tjr, " +
                " gs.jc||\'财务部审核\' as dqzt,sk.id as id from t_biz_sfk sk " +
                "left join t_biz_sfk fk on fk.nbhzsfk_id = sk.id " +
                "left join t_sys_yg yg on yg.id = sk.createuser_id " +
                "left join t_sys_gsjg gs on gs.id = sk.sbgs_id  " +
                "where sk.zdhz = 1 and sk.deleted = 0 and sk.sfklx in (3,4) and sk.zt = \'ytj\' " +
                " and fk.gldw_id = ? ";
        return (List<Map>)createSQLQuery(sql, gsid).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();

    }
}
